import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'package:toys_flutter/components/MyIconButton.dart';
import 'package:toys_flutter/pages/AttachmentUsers.dart';
import 'package:toys_flutter/pages/DynamicList.dart';
import 'package:toys_flutter/pages/EditDynamic.dart';
import 'package:toys_flutter/pages/ReferUsers.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with TickerProviderStateMixin {
  late final TabController _tabController =
      TabController(length: 3, vsync: this);

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    _tabController.dispose(); // 释放控制器
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    const Color colorMain = Color.fromRGBO(255, 163, 158, 1);

    return Scaffold(
      appBar: AppBar(
        title: SizedBox(
          width: 250,
          child: TDTabBar(
            indicatorPadding: EdgeInsets.zero,
            labelPadding: EdgeInsets.zero,
            tabs: const [
              TDTab(text: '动态市场'),
              TDTab(text: '推荐用户'),
              TDTab(text: '附近用户'),
            ],
            controller: _tabController,
            showIndicator: true,
            dividerHeight: 0,
          ),
        ),
        backgroundColor: colorMain,
        actions: [
          Padding(
            padding: const EdgeInsets.only(right: 15),
            child: _tabController.index == 0
                ? TextButton(
                    style: ButtonStyle(
                      backgroundColor:
                          WidgetStateProperty.all<Color>(Colors.white),
                      padding: WidgetStateProperty.all<EdgeInsets>(
                          const EdgeInsets.symmetric(
                              horizontal: 10, vertical: 6)),
                      minimumSize:
                          WidgetStateProperty.all<Size>(const Size(0, 0)),
                    ),
                    onPressed: () {
                      // 跳转到发动态页面
                      Get.to(const EditDynamicPage());
                    },
                    child: const Row(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Icon(
                          Icons.send,
                          size: 12,
                          color: Colors.black,
                        ),
                        SizedBox(width: 2),
                        Text(
                          '发动态',
                          style: TextStyle(fontSize: 12, color: Colors.black),
                        ),
                      ],
                    ),
                  )
                : const Row(
                    mainAxisSize: MainAxisSize.min,
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: [
                      MyIconButton(icon: Icons.search),
                      MyIconButton(icon: Icons.my_location),
                    ],
                  ),
          ),
        ],
      ),
      body: Container(
        alignment: Alignment.topLeft, // 设置左对齐
        decoration: const BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
            colors: [
              colorMain,
              Colors.white,
              Colors.white,
              Colors.white,
            ],
          ),
        ),
        child: TabBarView(
          controller: _tabController, // 添加控制器
          physics: const NeverScrollableScrollPhysics(),
          children: const [
            DynamicList(),
            ReferUsers(),
            AttachmentUsers(),
          ],
        ),
      ),
    );
  }
}
